package com.example.wzh.robotsdemo;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.TextView;

import butterknife.ButterKnife;
import butterknife.InjectView;

public class MainActivity extends AppCompatActivity {


    @InjectView(R.id.t1)
    TextView t1;
    @InjectView(R.id.t2)
    TextView t2;
    @InjectView(R.id.t3)
    TextView t3;
    @InjectView(R.id.t4)
    TextView t4;
    @InjectView(R.id.t5)
    TextView t5;
    @InjectView(R.id.t6)
    TextView t6;

    private LocalBroadcastManager manager;
    private MyReceiver myReceiver;
    private static final String TAG = "MainActivity";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.inject(this);

        //注册广播接收器接收更新数据
        manager = LocalBroadcastManager.getInstance(MainActivity.this);
        IntentFilter filter = new IntentFilter();
        filter.addAction("com.example.wzh.robotsdemo.MainActivity.MyReceiver");
        myReceiver = new MainActivity.MyReceiver();
        manager.registerReceiver(myReceiver, filter);

//        startMyServer();



         //   loginjzy();



    }

//    public void loginjzy() {
//
//        User user = new User(LoginUtils.USER_NAME, LoginUtils.PASS_WORD, LoginUtils.LANG);
//        Log.e("TAG","USERJson===" + user);
//        OkHttpUtils
//                .postString()
//                .url(LoginUtils.LOGIN_URL)
//                .content(new Gson().toJson(user))
//                .addHeader("Accept", LoginUtils.ACCEPT)
//                .addHeader("X-Gizwits-Application-Id", LoginUtils.APPID)
//                .build()
//                .execute(new MyStringCallback());
//    }


//    private void startMyServer() {
//        Intent startIntent = new Intent(this, MyService.class);
//        startService(startIntent);
//    }

    /**
     * 接收广播更新视图
     */
    private class MyReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.e("TAA","intentAcction" + intent.getAction());
            double room1_modbus_temperature_sensor = intent.getIntExtra("room1_modbus_temperature_sensor", -1);
            int room1_modbus_co2_sensor = intent.getIntExtra("room1_modbus_co2_sensor", -1);
            int room1_modbus_pm25_sensor = intent.getIntExtra("room1_modbus_pm25_sensor", -1);
            double room1_modbus_voc_sensor = intent.getIntExtra("room1_modbus_voc_sensor", -1);
            double room1_modbus_humidity_sensor = intent.getIntExtra("room1_modbus_humidity_sensor", -1);
            double room1_modbus_HCHO_sensor = intent.getIntExtra("room1_modbus_HCHO_sensor", -1);
            t1.setText(room1_modbus_HCHO_sensor/100 + "");
            t2.setText(room1_modbus_co2_sensor + "");
            t3.setText(room1_modbus_pm25_sensor + "");
            t4.setText(room1_modbus_voc_sensor/100 + "");
            t5.setText(room1_modbus_temperature_sensor/10 + "");
            t6.setText(room1_modbus_humidity_sensor/10 + "");

        }
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        manager.unregisterReceiver(myReceiver);
//        try {
//            SpeechManager.getInstance().closeSpeechDiscern(this);
//            SpeechManager.getInstance().removeSpeechState(this, 11);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }

    }
//    private class MyStringCallback extends Callback<String> {
//        @Override
//        public String parseNetworkResponse(Response response, int id) throws Exception {
//            return response.body().string();
//        }
//
//        @Override
//        public void onError(Call call, Exception e, int id) {
//            Log.e("TAG","登录的联网失败" + e.getMessage());
//        }
//
//        @Override
//        public void onResponse(String response, int id) {
//            Log.e("TAG","登录的联网成功" + response);
//            LoginBean bean = new Gson().fromJson(response, LoginBean.class);
//            String token = bean.getToken();
//            SpUtils.putString(MainActivity.this,"token",token);
//        }
//
//    }
}
